#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "Dac.h"
#include"fileread.h"
#include <fcntl.h>
#include <stdio.h>
using namespace std;
int flag = 0;

QString  data[10] = {"111","222","333"};
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //filewrite("write data");
    //fileread();

    if(flag == 0)
    {
//        system("echo rpmsg_kernel_CM4.elf > /sys/class/remoteproc/remoteproc0/firmware");

//        system("echo start > /sys/class/remoteproc/remoteproc0/state");
        flag = 1;
    }

   connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendData()));
   connect(this,SIGNAL(readM4_signal()),this,SLOT(readM4()));
   int val = 1;
   m_fd = open("/dev/rp", O_RDWR);
   if(m_fd < 0)
   {
       printf("can't open!\n");
       return;
   }
   else
       qDebug("open file OK");

       //close(fd);
    //lock = 0;
    //if(sign == 0)
        //share_init();
    //connect(this,SIGNAL(signal_showQstring()),this,SLOT(showQstring()));
    //M4_share();
    //timer = new QTimer();
    //timer->setInterval(500);
    //timer->start();


    //DAC.setDac("1111");
   mythread = new MyThread(this);
   mythread->start();
   //connect(ui->pushButton_dac,SIGNAL(clicked()),this,SLOT(setDac()));
    //connect(timer,SIGNAL(timeout()),this,SLOT(getAdcData()));
    //system("echo BEEP_CM4.elf > /sys/class/remoteproc/remoteproc0/firmware");
    // system("echo start > /sys/class/remoteproc/remoteproc0/state");

}

MainWindow::~MainWindow()
{
    delete ui;
    delete timer;
    system("echo stop > /sys/class/remoteproc/remoteproc0/state");
    shmdt(ptext);//解映射
    shmctl(shmid, IPC_RMID, NULL);//删除共享内存
    mythread->quit();
    mythread->exit(0);
    mythread->terminate();
    flag = 0;
}

QString MainWindow::readADC()
{
    char const *filename = "/sys/devices/platform/soc/48003000.adc/48003000.adc:adc@0/iio:device0/in_voltage19_raw";

    QFile   fd(filename);
    fd.open(QIODevice::ReadWrite);
    QByteArray t = fd.readAll();
    QString adcValue = QString(t);

    QStringList list = adcValue.split("\n");
    return list[0];
}

QString MainWindow::adcData()
{
    return adcdata;
}

void MainWindow::getAdcData()
{

    adcdata = readADC();

    double temp_data = adcdata.toDouble() * 0.050354003;
    adcdata = QString::number(temp_data,'f',1);
   // qDebug("buf = %s",qPrintable(adcdata));
    ui->label_adc->setText(adcdata+"mv");
    emit adcDataChanged();
}

void MainWindow::setCapture(bool str)
{
    if(str)
        timer->start(500);
    else
        timer->stop();

}

void MainWindow::setDac()
{
    qDebug("**********");
    char const *filename = "/sys/devices/platform/soc/40017000.dac/40017000.dac:dac@1/iio:device1/out_voltage1_raw";
    QFile   fd(filename);
    fd.open(QIODevice::ReadWrite);
    int err = 0;
    QByteArray ba;
    QString dac = ui->pushButton_dac->text();
    ba = dac.toLatin1();
    char *buf = ba.data();

    qDebug()<<dac;
    qDebug()<<ba;
    qDebug(buf);
    err = fd.write(ba,sizeof(ba));

    fd.close();
}

void MainWindow::share_init()
{
    key = ftok("/dev/ttyRPMSG",NULL);
    //创建共享内存
    shmid = shmget(key, 1024, 0640 | IPC_CREAT);
    if (shmid == -1)
    {
        printf("create shmget error:%s\n", errno);
    }
    ptext = NULL;   // 用于指向共享内存的指针
     // 将共享内存连接到当前进程的地址空间，由ptext指针指向它
    ptext = (char*)shmat(shmid, NULL, 0);//0表示共享内存可读可写
    if (ptext == (void*)-1)
    {
        perror("shmat");
        exit(1);
    }
    int nn = 0;
    sign = 1;
    sprintf(ptext, "start");//写入共享内存
}

void MainWindow::showQstring()
{

    qDebug("read data: %s\n", ptext);
    ui->label_share->setText(ptext);
    sprintf(ptext, "OK");//写入共享内存
    sleep(1);
}
void MainWindow::test()
{
    qDebug("read data: %s\n", ptext);
    ui->label_share->setText(ptext);
//        if (!strncmp(ptext, "quit", 4))
//            break;
    sprintf(ptext, "OK");//写入共享内存
    sleep(1);
}

void MainWindow::M4_share()
{

    //echo 8 > /proc/sys/kernel/printk && echo rpmsg_kernel_CM4.elf > /sys/class/remoteproc/remoteproc0/firmware && echo start > /sys/class/remoteproc/remoteproc0/state
   //system("echo UART_CM4.elf > /sys/class/remoteproc/remoteproc0/firmware");
   //system("echo start > /sys/class/remoteproc/remoteproc0/state");
    system("insmod rpmsg_client_sample.ko");
    //system("modprobe rpmsg_client_sample");
    system("echo 8 > /proc/sys/kernel/printk");
    system("echo rpmsg_kernel_CM4.elf > /sys/class/remoteproc/remoteproc0/firmware");
    //system("echo LED_CM4.elf > /sys/class/remoteproc/remoteproc0/firmware");
    system("echo start > /sys/class/remoteproc/remoteproc0/state");


}

void MainWindow::sendData()
{
    QString text = ui->lineEdit_send->text();
    char buf1[1024] ;
    QByteArray ba = text.toLatin1();
   // buf1 = ba.data();
    strcpy(buf1,ba.data());
    num = 99;
    //qDebug()<<text;
    //qDebug()<<ba.data();
    qDebug("write data: %s",buf1);
    write(m_fd, buf1, strlen(buf1));
}

void MainWindow::readM4()
{
    char buf[1024] = {"0"};
    memset(buf,0,1024);
    //printf("0buf = %d\r\n",num);

    read(m_fd,buf,1024);

    if(strlen(buf) != 0)
    {
         ui->textBrowser_M4->setText(buf);
         qDebug("read data = %s\r\n",buf);
    }
}
void MainWindow::Readfile()
{
   // QFile();

}


void MyThread::run()
{
    while(1)
    {
        //emit t->signal_showQstring();
        emit t->readM4_signal();
        msleep(50);
    }
}
